//
//  Base64DecoderTests.swift
//  AlphaWalletTests
//
//  Created by Vladyslav Shepitko on 25.04.2023.
//

import XCTest
@testable import AlphaWallet
import Foundation
import AlphaWalletCore
import AlphaWalletFoundation

final class Base64DecoderTests: XCTestCase {

    func testNil() throws {
        let decoder = Base64Decoder()
        XCTAssertNil(decoder.decode(string: "hello"))
    }

    func testValidBase64String() throws {
        let decoder = Base64Decoder()
        let res = decoder.decode(string: "data:application/json;base64,ewogICJuYW1lIjogIk5GVFdvcmxkcyBBdmF0YXIgTWludCBQYXNzIiwKICAiZGVzY3JpcHRpb24iOiAiQnkgW1l1Z2FMYWJzXShodHRwczovL29wZW5zZWEuaW8vWXVnYUxhYnMpXHJcblxyXG5QYXNzIHRvIHRha2UgcGFydCBpbiBORlRXb3JsZHMgQXZhdGFyIE1pbnQuXHJcblxyXG5UaGVzZSBhdmF0YXJzIHdpbGwgY292ZXIgdGhlIGludGVybmV0IGZvciBodW5kcmVkcyBvZiB5ZWFycy5cclxuXHJcbk5vdyBpcyB5b3VyIGNoYW5jZSB0byBnZXQgYXZhdGFyIGZvciBjZW50dXJ5IiwKICAiaW1hZ2UiOiAiaHR0cHM6Ly9pcGZzLmlvL2lwZnMvUW1YOTVvOG5Wd1I3a0djclRpeVU4b1N0ZGJudnFQTlRXWVA5UWQ4NXlOWXF4UCIsCiAgImV4dGVybmFsX3VybCI6ICJodHRwczovL25mdHdvcmxkYXZhdGFyLnh5eiIKfQ==")

        XCTAssertNotNil(res)
    }

    /// check case when base64 doesn't have trailing symbols ==
    func testValidBase64StringWithoutTrailingSymbols() throws {
        //NOTE: example of data when decoder doesn't work without additional check `|| Data(base64Encoded: string) != nil`
        let decoder = Base64Decoder()
            // swiftlint:disable line_length
            let string = "data:application/json;base64,eyJuYW1lIjogIkR1bmdlb24gIzc1ODUiLCAiZGVzY3JpcHRpb24iOiAiRHVuZ2VvbnMgKGZvciBBZHZlbnR1cmVycykgaXMgZ2VuZXJhdGl2ZSBhcnQgcHJvamVjdCB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